<template>
  <el-form
    :model="fromData"
    ref="ruleFormRef"
    label-width="auto"
    label-position="left"
    class="loginFrom"
    hide-required-asterisk
    :rules="rules"
  >
    <el-form-item label="手机号" prop="phone">
      <el-input type="text" v-model="fromData.phone" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <el-row :gutter="5">
        <el-col :span="15">
          <el-input type="text" v-model="fromData.code" placeholder="请输入验证码" />
        </el-col>
        <el-col :span="9">
          <img src="" alt="验证码" />
        </el-col>
      </el-row>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts" name="LoginPhone">
import { reactive, ref } from 'vue'

const fromData = reactive({
  phone: '',
  code: '',
})

const ruleFormRef = ref(null)

const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

// 定义 emit 事件
const emit = defineEmits(['submitForm'])

const handleSubmit = () => {
  ruleFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      emit('submitForm', fromData)
    }
  })
}
</script>
